package com.example.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.ChatFriendRel;
import com.example.domain.ChatUser;
import com.example.service.ChatFriendRelService;
import com.example.service.ChatUserService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 不是菜鸡爱编程
 * @Date: 2024/01/29/19:37
 * @Description:
 */
@RestController
@RequestMapping("/friend_rel")
public class ChatFriendRelController {
    @Autowired
    private ChatFriendRelService chatFriendRelService;
    @Autowired
    private ChatUserService chatUserService;

    /**
     * 查看用户好友关系列表
     *
     * @return {@link R}
     */
    @GetMapping("/list/{userId}")
    public R listByUserId(@PathVariable Integer userId) {
        // 找到编号userId用户的好友
        List<ChatFriendRel> list = chatFriendRelService.list(
                new LambdaQueryWrapper<ChatFriendRel>()
                        .eq(ChatFriendRel::getUserId, userId));
        // 判断好友列表是否为空
        if(CollUtil.isEmpty(list)){
            return R.ok();
        }
        // 获得好友编号
        List<Integer> friendIdList = list.stream().map(ChatFriendRel::getFriendId).collect(Collectors.toList());
        // 查找到好友信息
        List<ChatUser> friendUserList = chatUserService.list(new LambdaQueryWrapper<ChatUser>()
                .in(ChatUser::getId, friendIdList));
        return R.ok().setData(friendUserList);
    }
}
